package string;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 38917
 * Date: 2025/5/12
 * Time: 17:48
 */

public class String_Demo {
    //构建字符串
    public static void main1(String[] args) {
        //1.使用字符串常量进行赋值
        String string1 = "Hello World";
        System.out.println(string1);
        //2.new String
        String string2 = new String("Hello World");
        System.out.println(string2);
        //3.使用字符数组构造
        char[] chars = new char[]{'H','e','l','l','o',' ','W','o','r','l','d'};
        String string3 = new String(chars);
        System.out.println(string3);
        //4.使用字节数组构造
        byte[] bytes = new byte[]{72,101,108,108,111,32,87,111,114,108,100};
        String string4 = new String(bytes);
        System.out.println(string4);
    }
    //字符串存储原理
    public static void main2(String[] args) {
        String string1 = "Hello World";
        String string2 = "Hello World";
        String string3 = new String("Hello World");
        System.out.println(string1 == string2);//true
        System.out.println(string1 == string3);//false
    }
    //使用==进行比较
    public static void main3(String[] args) {
        int a = 1;
        int b = 1;
        int c = 2;
        System.out.println(a == b);//true
        System.out.println(a == c);//false
        String string1 = new String("Hello World");
        String string2 = new String("Hello World");
        System.out.println(string1 == string2);//false
    }
    //使用equals方法进行比较
    public static void main4(String[] args) {
        String string1 = new String("Hello World");
        String string2 = new String("Hello World");
        String string3 = new String("hello world");
        System.out.println(string1.equals(string2));//true
        System.out.println(string1.equals(string3));//false
    }
    //使用compareTo方法进行比较
    public static void main5(String[] args) {
        String string1 = new String("Hello World");
        String string2 = new String("Hello World");
        String string3 = new String("hello world");
        String string4 = new String("Hello");
        System.out.println(string1.compareTo(string2));//返回0
        System.out.println(string1.compareTo(string3));//返回-32,'H' - 'h'
        System.out.println(string1.compareTo(string4));//返回6,string1比string4长6个字符
    }
    //charAt
    public static void main6(String[] args) {
        String string = "0123456789";
        System.out.println(string.charAt(0));//0
        System.out.println(string.charAt(100));//StringIndexOutOfBoundsException
        System.out.println(string.charAt(-1));//StringIndexOutOfBoundsException
    }
    //indexOf
    public static void main7(String[] args) {
        String string = "HelloWorld";
        //H的Unicode值:72
        System.out.println(string.indexOf(72));//返回H的下标0
        System.out.println(string.indexOf(1));//返回-1
    }
    //indexOf
    public static void main8(String[] args) {
        String string = "HelloHello";
        //H的Unicode值:72
        System.out.println(string.indexOf(72, 3));//返回第二个H的下标5
    }
    //indexOf
    public static void main9(String[] args) {
        String string = "HelloWorld";
        //H的Unicode值:72
        System.out.println(string.indexOf("H"));//返回H的下标0
        System.out.println(string.indexOf('H'));//返回H的下标0
        System.out.println(string.indexOf("Z"));//返回-1
        System.out.println(string.indexOf('Z'));//返回-1
    }
    //indexOf
    public static void main10(String[] args) {
        String string = "HelloHello";
        System.out.println(string.indexOf("H", 3));//返回第二个H的下标5
    }
    //valueOf
    public static void main11(String[] args) {
        String string1 = String.valueOf(10);
        System.out.println(string1);//10
        String string2 = String.valueOf(10.0);
        System.out.println(string2);//10.0
        String string3 = String.valueOf(true);
        System.out.println(string3);//true
    }
    public static void main12(String[] args) {
        int data1 = Integer.parseInt("12345");
        System.out.println(data1);//12345
        double data2 = Double.parseDouble("12345.0");
        System.out.println(data2);//12345.0
        boolean data3 = Boolean.parseBoolean("true");
        System.out.println(data3);//true
    }
    //String toUpperCase()
    //String toLowerCase()
    public static void main13(String[] args) {
        String string1 = "Hello World";
        System.out.println(string1.toLowerCase());//hello world
        String string2 = "Hello World";
        System.out.println(string2.toUpperCase());//HELLO WORLD
    }
    //toCharArray
    public static void main14(String[] args) {
        String string = "Hello World";
        char[] array = string.toCharArray();
        for (char c : array) {
            System.out.print(c + " ");
        }
        //输出结果：H e l l o   W o r l d
        System.out.println();
    }
    //format
    public static void main15(String[] args) {
        String name = "Alice";
        int age = 30;
        String result1 = String.format("Name: %s, Age: %d", name, age);
        System.out.println(result1);//Name: Alice, Age: 30
        String result2 = String.format("%d-%d-%d", 2025, 6,12);
        System.out.println(result2);//2025-6-12
    }
    //replaceAll
    //replaceFirst
    public static void main16(String[] args) {
        String string = "Hello World";
        String string1 = string.replaceAll("l", "_");
        System.out.println(string1);//He__o Wor_d
        String string2 = string.replaceFirst("l", "_");
        System.out.println(string2);//He_lo World
    }
    //split
    public static void main17(String[] args) {
        String string1 = "Hello World";
        String[] array1 = string1.split(" ");
        for (String s : array1) {
            System.out.println(s);
        }
        //Hello
        //World
        String string2 = "Hello World Hello World";
        String[] array2 = string2.split(" ", 2);
        for (String s : array2) {
            System.out.println(s);
        }
        //Hello
        //World Hello World
    }
    //substring
    public static void main18(String[] args) {
        String string = "Hello World";
        String substring1 = string.substring(1);//[1,end)
        System.out.println(substring1);//ello World
        String substring2 = string.substring(1, 2);//[1,2)
        System.out.println(substring2);//e
    }
    //trim()
    public static void main19(String[] args) {
        String string = "   Hello World   ";
        String trim = string.trim();
        System.out.println("[" + trim + "]");//[Hello World]
    }
    //intern()
    public static void main(String[] args) {
        char[] ch = new char[]{'a','b','c'};
        String str1 = new String(ch);
        //str1.intern();
        String str2 = "abc";
        System.out.println(str1 == str2);
    }
}
